package com.xy.blog.article.domain.controller;

import com.xy.blog.article.api.ArticleCategoryService;
import com.xy.blog.article.api.model.ArticleCategoryDTO;
import com.xy.blog.article.api.model.ArticleCategoryQuery;
import com.xy.blog.article.domain.mapstruct.ArticleCategoryConverter;
import com.xy.blog.article.domain.service.IArticleCategoryService;
import com.xy.blog.article.domain.service.model.ArticleCategoryBO;
import com.xy.blog.core.exception.BaseException;
import com.xy.blog.core.springmvc.controller.BaseController;
import com.xy.blog.core.springmvc.result.page.PageReq;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author zanhonglei
 * @version V1.0
 * @Description:
 * @date 2020/5/24
 */
@Api(value = "ArticleCategoryController",tags = "文章类别")
@RestController
public class ArticleCategoryController extends BaseController implements ArticleCategoryService {

    @Autowired
    private IArticleCategoryService articleCategoryService;

    @Autowired
    private ArticleCategoryConverter articleCategoryConverter;

    @Override
    public Object pages(PageReq<ArticleCategoryQuery> req) throws BaseException {
        return getPageData(articleCategoryService.listByPage(req));
    }

    @Override
    public Object getById(Long id) throws BaseException {
        return getData(articleCategoryService.getById(id));
    }

    @Override
    public Object save(@Valid ArticleCategoryDTO req) throws BaseException {
        ArticleCategoryBO articleCategoryBO = articleCategoryConverter.req2Bo(req);
        articleCategoryService.save(articleCategoryBO);
        return success();
    }

    @Override
    public Object update(@Valid ArticleCategoryDTO req) throws BaseException {
        ArticleCategoryBO articleCategoryBO = articleCategoryConverter.req2Bo(req);
        articleCategoryService.update(articleCategoryBO);
        return success();
    }

    @Override
    public Object deleteById(Long id) throws BaseException {
        articleCategoryService.deleteById(id);
        return success();
    }

    @Override
    public Object list() throws BaseException {
        return getData(articleCategoryService.list());
    }

    @Override
    public Object listWithChildren(Long level) throws BaseException {
        return getData(articleCategoryService.listWithChildrenByLevel(level));
    }


}
